<template>
    <div style="width: 1000px; margin-left: 10px">
        <el-input
                placeholder="请输入部门名称进行搜索..."
                prefix-icon="el-icon-search"
                v-model="filterText">
        </el-input>

        <el-tree
                default-expand-all
                show-checkbox
                class="tre"
                :data="deps"
                :props="defaultProps"
                :filter-node-method="filterNode"
                :expand-on-click-node="false"
                ref="tree">
            <span class="custom-tree-node" style="display: flex;justify-content: space-between;width: 100%" slot-scope="{ node, data }">
                <span>{{ data.name }}</span>
                    <span>
                      <el-button
                              type="primary"
                              size="mini"
                              class="depBtn"
                              @click="() => showAddDepView(data)">
                        添加部门
                      </el-button>
                      <el-button
                              type="danger"
                              size="mini"
                              class="depBtn"
                              @click="() => deleteDep(data)">
                        删除部门
                      </el-button>
                    </span>
          </span>
        </el-tree>
        <el-dialog
                title="添加部门"
                :visible.sync="dialogVisible"
                width="30%">
            <div>
                <table>
                    <tr>
                        <td><el-tag>上级部门</el-tag></td>
                        <td>{{pname}}</td>
                    </tr>
                    <tr>
                        <td><el-tag>部门名称</el-tag></td>
                        <el-input v-model="dep.name" placeholder="请输入部门名称..."></el-input>
                    </tr>
                </table>
            </div>
            <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="doAddDep">确 定</el-button>
  </span>
        </el-dialog>

    </div>
</template>

<script>
    export default {
        name: "DepMana",
        data() {
            return{
                dialogVisible: false,
                filterText: '',
                deps: [], //显示的对象
                dep: {    //添加的对象
                    name: '',
                    parentId: -1
                },
                pname: '', //不需要后端提交 这哩拿来显示 父节点(即上级部门)的名字
                defaultProps: {
                    children: 'children',
                    label: 'name'
                }
            }
        },
        watch: {
            filterText(val) {
                this.$refs.tree.filter(val);
            }
        },
        mounted() {
            this.initDeps()
        },
        methods: {
            /*value 是传进来的val值 data是json数组中的一项*/
            filterNode(value, data) {
                if (!value) return true;
                /*如果下标不等于-1说明有值*/
                return data.name.indexOf(value) !== -1;
            },
            initDeps() {
                this.getRequest("/institute/department/").then(res => {
                    if (res) {
                        this.deps = res
                    }
                })
            },
            showAddDepView(data) {
                this.pname = data.name
                this.dep.parentId = data.id
                this.dialogVisible = true
            },
            //重置刷新输入的数据 初始化变量
            initDep() {
                this.dep = {
                    name: '',
                    parentId: -1
                }
                this.pname = ''
            },
            //给数组重新赋值 达到树tree的更新操作
            addDep2Deps(deps, dep) {
                for (let i=0; i<deps.length; i++) {
                    let d = deps[i]
                    if (d.id === dep.parentId) {
                        d.children = d.children.concat(dep) //concat 数组添加函数
                        if (d.children.length > 0) {
                            p.parent = true //此时是父部门
                        }
                        return //到此为止不用执行
                    } else {
                        this.addDep2Deps(d.children, dep)
                    }
                }
            },
            doAddDep() {
                this.postRequest("/institute/department/", this.dep).then(res => {
                    if (res) {
                        this.dialogVisible = false //先关闭再刷新 否则无法关闭
                        // this.initDeps() //这样子每次提交成功后tree就会收起来
                        this.addDep2Deps(this.deps, res.obj)
                        this.initDep()
                    }
                })
            },
            removeDepFromDeps(p,deps, id) {
                for(let i=0;i<deps.length;i++){
                    let d = deps[i];
                    if (d.id === id) {
                        deps.splice(i, 1);
                        if (deps.length === 0) {
                            p.parent = false; //不再是父部门 可以删除
                        }
                        return;
                    }else{
                        this.removeDepFromDeps(d,d.children, id);
                    }
                }
            },
            deleteDep(data) {
                if (data.isParent) {
                    this.$message.error("父部门删除失败");
                } else {
                    this.$confirm('此操作将永久删除【' + data.name + '】部门, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        this.deleteRequest("/institute/department/" + data.id).then(res=>{
                            if (res) {
                                this.removeDepFromDeps(null,this.deps,data.id);
                            }
                        })
                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消删除'
                        });
                    });
                }
            },
        },
    }
</script>

<style>
    .depBtn{
        padding: 2px;
    }
    .tre{
        margin-top: 10px;
        width: 1000px;
    }
</style>
